package com.mycompany.goodsys.core;

import com.mycompany.goodsys.basecom.Observer.*;

public class MainView {
    private static int opt = 0;

    public static void Init(){
        // 2.注册监听事件
        // 3.进入相应模块
        opt = 0;
        EventSystem.BindEvent(EventName.EnterKey,(Object[] objs)->{
            String key = objs[0].toString();
            if(key.equals("w")){
                ++opt;
                opt %= 4;
            }

            if(key.equals("s")){
                --opt;
                if(opt < 0) opt = 3;
            }

            if(key.equals("r")) {
                switch(opt){
                    case 0:
                        AppStateCtrl.ChangeState(AppStateCtrl.AppState.AddGoodView);
                        break;
                    case 1:
                        AppStateCtrl.ChangeState(AppStateCtrl.AppState.ChangeGoodView);
                        break;
                    case 2:
                        AppStateCtrl.ChangeState(AppStateCtrl.AppState.RemoveGoodView);
                        break;
                    case 3:
                        AppStateCtrl.ChangeState(AppStateCtrl.AppState.SearchGoodView);
                        break;
                }

            }
            //System.out.println(" key: " + key + "  opt: " + opt +  "   " +  key.equals("w") + "   " + key.equals("s"));
        });
    }

    // 1.显示操作界面
    public static void Update(){
        String str = "提示：使用 w(上) s(下) r(确定) 进行选择操作\n" +
                "%2s\t1.进入【添加】界面\n" +
                "%2s\t2.进入【修改】界面\n" +
                "%2s\t3.进入【移除】界面\n" +
                "%2s\t4.进入【搜索】界面";

        int temp_opt = opt + 1;
        String s1 = temp_opt == 1 ? "→" : "  ";
        String s2 = temp_opt == 2 ? "→" : "  ";
        String s3 = temp_opt == 3 ? "→" : "  ";
        String s4 = temp_opt == 4 ? "→" : "  ";

        System.out.println(String.format(str,s1,s2,s3,s4));
    }

    public static void Out(){
        EventSystem.RemoveEvent(EventName.EnterKey);
    }

}
